import {Component, EventEmitter, Input, Output} from "@angular/core";
import {ActivatedRoute, Router} from "@angular/router";
import {NzNotificationService} from "ng-zorro-antd";
import {TrainingPlanDetailBean} from "../../bean/trainingPlanDetail.bean";
import {Config} from "../../../../service/config";
import {HttpServcie} from "../../../../service/http.service";
import {FormBuilder,FormGroup,Validators} from "@angular/forms";
import * as moment from "moment";

@Component({
  selector: "trainingPlan-detail-update-page",
  templateUrl: "trainingPlan-detail-update.html",
  styleUrls: ["trainingPlan-detail-update.scss"]
})

export class TrainingPlanDetailUpdate {
  trainingPlanDetail : TrainingPlanDetailBean = new TrainingPlanDetailBean();
  config = Config;
  validateForm: FormGroup;
  @Input() inValue: any = [];
  @Output() outValue = new EventEmitter();
  constructor(
    private httpService: HttpServcie,
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private _notification: NzNotificationService,
    private fb: FormBuilder
  ) {
  }

  ngOnInit(){
    if (this.inValue) {
      this.trainingPlanDetail = Object.assign({}, this.inValue);
    }
    this.validateForm = this.fb.group({
      partyName: [null, [Validators.required]], //计划名称
      partyType: [null, [Validators.required]], //计划类型
      leadUnit: [null, [Validators.required]], //计划描述
      unitManageUser: [null, [Validators.required]], //牵头单位
      adjustReason: [null, [Validators.required]], //负责人
      adjustDate: [null, [Validators.required]], //开始时间
    });
  }

  submitForm()  {
    this.trainingPlanDetail.adjustDate = moment(this.trainingPlanDetail.adjustDate).format("YYYYMMDD");
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
    }
    //处理是否提交
    for (const i in this.validateForm.controls) {
      if (!this.validateForm.controls[i].valid) {
        return;
      }
    }
    this.httpService.updateTrainingPlanDetail(this.trainingPlanDetail).subscribe(data => {
      if (data["status"] != "200") {
        this._notification.create("error", "错误提示", "修改失败！");
      }
      if (data["status"] === "200") {
        this._notification.create("success", "", "修改成功！");
      }
      this.outValue.emit(true);
    });
  }

  quit() {
    this.outValue.emit(true);
  }
  // 查询面板
  panels = {
    active: true,
    name: "修改",
    disabled: false,
    childPanel: [
      {
        active: false,
        name: "修改"
      }
    ]
  };

  getFormControl(name) {
    return this.validateForm.controls[name];
  }
}
